package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.FullyLinearLayoutManager;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemLookAtBinding;
import com.jinlejia.erp.ui.common.EvaluateDialog;
import com.jinlejia.erp.utils.DateUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Map;

/**
 * 带看（Adapter）
 */

public class LookatAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;

    public LookatAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_look_at, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Map<String, Object> map = mData.get(position);
        ItemLookAtBinding binding = DataBindingUtil.getBinding(holder.itemView);
        binding.time.setText("带看时间: "+ DateUtil.getTime(map.get("showStartTime").toString(), "MM-dd HH:mm") + " - " + DateUtil.getTime(map.get("showEndTime").toString(), "HH:mm"));
        if ("1".equals(map.get("isEvaluate").toString())) {
            binding.isEvaluate.setText("客户已评价");
            binding.isEvaluate.setTextColor(context.getResources().getColor(R.color.green));
            binding.isEvaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EvaluateDialog evaluateDialog = new EvaluateDialog(context, Integer.parseInt(map.get("level").toString()), map.get("evaluateInfo").toString());
                    evaluateDialog.show();
                }
            });
        } else {
            binding.isEvaluate.setText("客户未评价");
            binding.isEvaluate.setTextColor(context.getResources().getColor(R.color.red));
        }

        binding.daikanren.setText("带看人: "+ map.get("userName"));
        binding.daikanzj.setText("带看总结: " + map.get("summary"));
        binding.innerRecycler.setLayoutManager(new FullyLinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        JSONArray houseList = JSON.parseArray(map.get("houseList").toString());
        //数据列表
        final ArrayList<Map<String, Object>> list = JSON.parseObject(houseList.toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
        });
        final InnerAdapter adapter = new InnerAdapter(new ArrayList<Map<String, Object>>());
        binding.innerRecycler.setAdapter(adapter);
        if( list.size()<=1 ){
        //只有一条带看
            binding.detailBtn.setVisibility(View.GONE);
            adapter.setData(list);
        }else{
            //多条带看默认显示一条
            binding.detailBtn.setVisibility(View.VISIBLE);
            final ArrayList<Map<String, Object>> dList = new ArrayList<>();
            dList.add(list.get(0));
            adapter.setData(dList);
            binding.detailBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (adapter.getData().size()==1) {
                        adapter.setData(list);
                    }else{
                        adapter.setData(dList);
                    }
                }
            });
        }



        holder.itemView.setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }
}
